package com.global.gbinventory.data.controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JOptionPane;

import com.global.gbinventory.barang.util.CONSTANT;
import com.global.gbinventory.data.view.SupplierFrame;
import com.global.gbinventory.exception.ExistedDataException;
import com.global.gbinventory.exception.StarFieldsNotFilledException;
import com.global.gbinventory.main.controller.MainController;

public class SupplierController {

	private MainController main;
	private SupplierListController listController;
	private SupplierFrame frame;
	
	private String oldKode;
	
	public SupplierController(MainController main){
		this.main = main;
		oldKode = CONSTANT.EMPTY_STRING;
	}
	
	public void start(){
		initialize();
		frame.displayFrame(true);
		main.addFrameToDesktopPane(frame);
		frame.moveToFront();
	}
	
	private void initialize(){
		this.frame = new SupplierFrame();
		this.listController = main.getSupplierListController();
		addListeners();
	}
	
	private void addListeners(){
		frame.addBtnNewListener(new BtnNewListener());
		frame.addBtnEditListener(new BtnEditListener());
		frame.addBtnDeleteListener(new BtnDeleteListener());
		frame.addBtnSaveListener(new BtnSaveListener());
		frame.addBtnCancelListener(new BtnCancelListener());
		frame.addBtnShowListListener(new BtnShowListListener());
	}
	
	class BtnNewListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			frame.setTextFieldEditable();
			frame.requestFocusTxtFldKode();
			listController.setSelectedRow(-1);
			activateSave();
			
			frame.setProvinsi("Sumatera Utara");
		}
		
	}
	
	class BtnEditListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			oldKode = frame.getKode();
			frame.setTextFieldEditable();
			frame.requestFocusTxtFldKode();
			activateSave();
		}
		
	}
	
	class BtnDeleteListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			String title = "Konfirmasi Penghapusan";
			String message = "Anda yakin mau menghapus supplier \"" + frame.getNama() + " \" ?";
			int ans = JOptionPane.showConfirmDialog(frame, message, title, JOptionPane.YES_NO_OPTION);
			
			if(ans == JOptionPane.YES_OPTION){
				listController.removeSupplier();
				
				title = "Penghapusan Berhasil";
				message = "Supplier \"" + frame.getNama() + " \" berhasil dihapus.";
				JOptionPane.showMessageDialog(frame, message, title, JOptionPane.INFORMATION_MESSAGE);
				
				cancel();
			}
		}
		
	}
	
	class BtnSaveListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			String kode = frame.getKode();
			String nama = frame.getNama();
			String alamat = frame.getAlamat();
			String kota = frame.getKota();
			String kodepos = frame.getKodePos();
			String provinsi = frame.getProvinsi();
			String telepon = frame.getTelepon();
			String namaagen = frame.getNamaAgen();
			String hpagen = frame.getHpAgen();
			String emailagen = frame.getEmailAgen();
			String keterangan = frame.getKeterangan();
			
			try{
				if(!isAllowedKode(kode)){
					throw new ExistedDataException();
				}
				if(!isStarFieldsFilled()){
					throw new StarFieldsNotFilledException();
				}
				
				listController.updateSupplier(kode, nama, alamat, kota, kodepos, 
						provinsi, telepon, namaagen, hpagen, emailagen, keterangan);
				
				String title = "Save Supplier Berhasil";
				String message = "Data supplier berhasil disimpan";
				JOptionPane.showMessageDialog(frame, message, title, JOptionPane.INFORMATION_MESSAGE);
				
				cancel();
				
			} catch(ExistedDataException e1){
				e1.printError(frame);
				
			} catch(StarFieldsNotFilledException e2){
				e2.printError(frame);
			} 
		
		}
		
	}
	
	class BtnCancelListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			cancel();
		}
		
	}
	
	class BtnShowListListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			listController.start(false);
		}
		
	}
	
	private boolean isAllowedKode(String kode){
		String existedKode = listController.isExistedKode(kode);
		if(oldKode.equalsIgnoreCase(existedKode) || 
				CONSTANT.EMPTY_STRING.equalsIgnoreCase(existedKode)){
			return true;
		}
		return false;
	}
	
	private boolean isStarFieldsFilled(){
		if(frame.getKode().equals(CONSTANT.EMPTY_STRING) || frame.getNama().equals(CONSTANT.EMPTY_STRING) 
				|| frame.getNamaAgen().equals(CONSTANT.EMPTY_STRING) || frame.getHpAgen().equals(CONSTANT.EMPTY_STRING)){
			return false;
		}
		
		return true;
	}
	
	public void fillData(String kode, String nama, String alamat, String kota, String kodepos,
			String provinsi, String telepon, String namaagen, String hpagen, 
			String emailagen, String keterangan){
		frame.setKode(kode);
		frame.setNama(nama);
		frame.setAlamat(alamat);
		frame.setKota(kota);
		frame.setKodePos(kodepos);
		frame.setProvinsi(provinsi);
		frame.setTelepon(telepon);
		frame.setNamaAgen(namaagen);
		frame.setHpAgen(hpagen);
		frame.setEmailAgen(emailagen);
		frame.setKeterangan(keterangan);
		
	}
	
	private void activateSave(){
		frame.setEnabledBtnNew(false);
		frame.setEnabledBtnEdit(false);
		frame.setEnabledBtnDelete(false);
		frame.setEnabledBtnSave(true);
		frame.setEnabledBtnCancel(true);
		frame.setEnabledBtnShowList(false);
	}
	
	public void activateEditDelete(){
		frame.setEnabledBtnNew(false);
		frame.setEnabledBtnEdit(true);
		frame.setEnabledBtnDelete(true);
		frame.setEnabledBtnSave(false);
		frame.setEnabledBtnCancel(true);
		frame.setEnabledBtnShowList(true);
	}
	
	private void cancel(){
		frame.clearTextField();
		frame.setTextFieldNonEditable();
		frame.setEnabledBtnNew(true);
		frame.setEnabledBtnEdit(false);
		frame.setEnabledBtnDelete(false);
		frame.setEnabledBtnSave(false);
		frame.setEnabledBtnCancel(false);
		frame.setEnabledBtnShowList(true);
		
		listController.setSelectedRow(-1);
	}
	
}
